package com.lc.schedule.controller;

import com.lc.schedule.common.Result;
import com.lc.schedule.dao.SysScheduleDao;
import com.lc.schedule.dao.impl.SysScheduleDaoImpl;
import com.lc.schedule.pojo.SysSchedule;
import com.lc.schedule.service.SysScheduleService;
import com.lc.schedule.service.impl.SysScheduleServiceImpl;
import com.lc.schedule.util.WebUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuchun
 */
@WebServlet("/schedule/*")
public class SysScheduleController extends BaseController {

    private SysScheduleService sysScheduleService = new SysScheduleServiceImpl();



    protected void deleteSchedule(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        //获取数据
        SysSchedule sysSchedule = WebUtil.readJson(request, SysSchedule.class);
        sysScheduleService.deleteSchedule(sysSchedule);
        WebUtil.writeJson(response,Result.ok(null));

    }

    protected void UpdateItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //post请求乱码，先设置一下解码方式，然后再接收数据
        request.setCharacterEncoding("UTF-8");
        //接收请求体里面的JSON数据，转换成对象
        SysSchedule sysSchedule = WebUtil.readJson(request, SysSchedule.class);
        //调用服务层方法，更新到数据库
        sysScheduleService.UpdateItem(sysSchedule);
        //响应
        WebUtil.writeJson(response,Result.ok(null));
    }

    protected void addSchedule(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取用户uid
        int uid = Integer.parseInt(request.getParameter("uid"));
        //调用service层方法进行增加
        sysScheduleService.addScheduleByUser(uid);
        Result result = Result.ok(null);
        WebUtil.writeJson(response,result);
    }

    protected void findAllSchedule(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //获取用户的uid
        int uid = Integer.parseInt(request.getParameter("uid"));
        //System.out.println(uid);
        //根据用户的uid查询用户
        Map data = new HashMap();
        List scheduleList = sysScheduleService.findSchedule(uid);
        data.put("scheduleList",scheduleList);
        Result result = Result.ok(data);
        WebUtil.writeJson(response,result);
    }

}
